<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts' // echarts theme
import resize from '@/framework/utils/mixins/resize'

require('echarts/theme/macarons')

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ expectedData, actualData } = {}) {
      this.chart.setOption({
        xAxis: {
          data: ['2023-01', '2023-02', '2023-03', '2023-04', '2023-05', '2023-06'],
          boundaryGap: false,
          axisTick: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: '#F5F6F9'
            }
          },
          axisLabel: {
            textStyle: {
              color: '#86909C'
            }
          }
        },
        grid: {
          left: 10,
          right: 30,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          // axisPointer: {
          //   type: 'cross'
          // },
          padding: [5, 10]
        },
        yAxis: {
          splitArea: { show: false }, // , areaStyle: { 'color': ['#fff'] }
          splitLine: { // 去除网格线
            show: true,
            lineStyle: {
              color: '#E5E8EF',
              type: 'dashed'
            }
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            textStyle: {
              color: '#86909C'
            }
          },
          axisLine: {
            show: false,
            lineStyle: {
              color: '#86909C'
              // type: 'dotted'
            }
          }
          // axisLable: {
          //   formatter: function(value) {
          //     return value + 'k'
          //   }
          // }
        },
        legend: {
          show: false
          // data: ['管理员', '普通用户']
        },
        series: [
          // {
          //   name: '管理员', itemStyle: {
          //     normal: {
          //       color: '#FF005A',
          //       lineStyle: {
          //         color: '#FF005A',
          //         width: 2
          //       }
          //     }
          //   },
          //   smooth: true,
          //   type: 'line',
          //   data: expectedData,
          //   animationDuration: 2800,
          //   animationEasing: 'cubicInOut'
          // },
          {
            name: '人数',
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                // color: '#3888fa',
                lineStyle: {
                  // color: '#3888fa',
                  color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ // 1代表上面
                    offset: 0,
                    color: '#6F41FA'
                  }, {
                    offset: 1,
                    color: '#1DE7FF'

                  }]),
                  width: 2
                },
                areaStyle: {
                  color: '#f3f8ff'
                }
              }
            },
            data: actualData,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          }]
      })
    }
  }
}
</script>
